;
define(function(require) {
	'use strict';
	var $ = require('jquery'),
		layer = require('layer'),
		echarts = require('echarts'),
		config = require('config'),
		$Fun = require('functions');

	function app() {}
	app.prototype = {
		init: function() {
			this.loadIndex = layer.load(2);

			var that = this;
			this.pageSize = this.getPageSize();
			this.newMapInit();
			this.baiduMapInit();

			setTimeout(function() {
				layer.close(that.loadIndex);
				$('#loadBg').remove();
			}, 500);
		}

		,
		newMapInit: function() {

			var that = this,
				$mapWrap = $('#newMap');

			var $baidu = $('.baidu-mb');
			if($baidu && $baidu.length > 0) {
				var name = '南通市',
					pinyin = 'nantongshi';
				$Fun.getJsonData(config.mapConfig.urlBasePath + config.map2code[name], function(returnJson) {
					echarts.registerMap(pinyin, returnJson);
					var $mapWrap = $('#newMap');
					if($mapWrap && $mapWrap.length > 0) {
						var mapChart = echarts.init($mapWrap[0]);
						var jsons = {
							"Data": {
								"areaData": [{
									"name": "启东市",
									"value": 68,
									"fwy": 22,
									"gy": 46
								}, {
									"name": "海门市",
									"value": 60,
									"fwy": 23,
									"gy": 37
								}, {
									"name": "开发区",
									"value": 43,
									"fwy": 17,
									"gy": 26
								}, {
									"name": "崇川区",
									"value": 41,
									"fwy": 32,
									"gy": 9
								}, {
									"name": "港闸区",
									"value": 38,
									"fwy": 28,
									"gy": 10
								}, {
									"name": "通州区",
									"value": 63,
									"fwy": 19,
									"gy": 44
								}, {
									"name": "如东县",
									"value": 65,
									"fwy": 24,
									"gy": 41
								}, {
									"name": "如皋市",
									"value": 74,
									"fwy": 30,
									"gy": 44
								}, {
									"name": "海安县",
									"value": 82,
									"fwy": 25,
									"gy": 57
								}],
								"colors": ["#c65152"],
								"map": pinyin,
								"mapName": name,
								"max": 82,
								"min": 38
							},
							"Success": true
						};
						$Fun.drawChartMap('', jsons.Data, function(rtn) {
							//更新数据
							var option = rtn;
							option.series[0].zoom = 1.2;
							option.series[0].top = 'middle';
							option.series[0].left = 'center';
							option.visualMap.left = 'left';
							option.visualMap.top = 'bottom';
							mapChart.setOption(option);

							//bind mapchart click event
							mapChart.on('click', function(params) {
								var $baidu = $('.baidu-mb');
								if($baidu && $baidu.length > 0) {
									location.href = 'baidumap.html';
									return false;
								}
							})
						})
					}
				})
			}
		}

		,
		baiduMapInit: function() {
			var that = this;
			var $baidu = $('.baidu-wx');
			if($baidu && $baidu.length > 0) {
				var map = new BMap.Map("map");
				$.get('assets/js/map.json', function(data) {
					for(var i = 0; i < data.length; i++) {
						if(data[i].distance_type == "FWY") {
							arrFWY.push(data[i]);
						} else if(data[i].distance_type == "GY") {
							arrGY.push(data[i]);
						}
					}
					showMap(data, that);
				});
			}
		}

		,
		getPageSize: function() {
			var xScroll, yScroll;
			if(window.innerHeight && window.scrollMaxY) {
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY
			} else {
				if(document.body.scrollHeight > document.body.offsetHeight) {
					xScroll = document.body.scrollWidth;
					yScroll = document.body.scrollHeight
				} else {
					xScroll = document.body.offsetWidth;
					yScroll = document.body.offsetHeight
				}
			}
			var windowWidth, windowHeight;
			if(self.innerHeight) {
				if(document.documentElement.clientWidth) {
					windowWidth = document.documentElement.clientWidth
				} else {
					windowWidth = self.innerWidth
				}
				windowHeight = self.innerHeight
			} else {
				if(document.documentElement && document.documentElement.clientHeight) {
					windowWidth = document.documentElement.clientWidth;
					windowHeight = document.documentElement.clientHeight
				} else {
					if(document.body) {
						windowWidth = document.body.clientWidth;
						windowHeight = document.body.clientHeight
					}
				}
			}
			if(yScroll < windowHeight) {
				var pageHeight = windowHeight
			} else {
				var pageHeight = yScroll
			}
			if(xScroll < windowWidth) {
				var pageWidth = xScroll
			} else {
				var pageWidth = windowWidth
			}
			var arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
			return arrayPageSize
		}
	}
	return app;
})